package br.com.gip.repository.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import br.com.gip.constant.Status;

@Entity
@Table(name="USUARIO")
public class Usuario implements Serializable {

	private static final long serialVersionUID = -6983370219020277936L;

	@Id
	@Column(name="USUARIO_ID")
	@SequenceGenerator(name="SQ_USUARIO_PK", sequenceName="SQ_USUARIO_PK", allocationSize=1)
	@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SQ_USUARIO_PK")
	private Long usuarioId;
	
	@Column(name="LOGIN", unique=true, nullable=false, length=64)
	private String login;
	
	@Column(name="PASSWORD", nullable=false, length=64)
	private String password;

	@Column(name="EMAIL", nullable=false, length=64)
	private String email;
	
	@Column(name="NOME", nullable=false, length=64)
	private String nome;
	
	@Enumerated(EnumType.STRING)
	@Column(name="STATUS_USUARIO", nullable=false)
	private Status statusUsuario;
	
    @ManyToOne
	@JoinColumn(name="DEPARTAMENTO_ID")
	private Departamento departamento;

    @ManyToOne
	@JoinColumn(name="CARGO_ID")
	private Cargo cargo;
	
	@ManyToOne(fetch=FetchType.EAGER)
	@JoinColumn(name="PERFIL_ACESSO_ID")		
    private PerfilAcesso perfilAcesso;
	
	public Usuario() {
		this.cargo = new Cargo();
		this.departamento = new Departamento();
		this.perfilAcesso = new PerfilAcesso();
	}
	
	public Long getUsuarioId() {
		return usuarioId;
	}

	public void setUsuarioId(Long usuarioId) {
		this.usuarioId = usuarioId;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public Status getStatusUsuario() {
		return statusUsuario;
	}

	public void setStatusUsuario(Status statusUsuario) {
		this.statusUsuario = statusUsuario;
	}

	public Departamento getDepartamento() {
		return departamento;
	}

	public void setDepartamento(Departamento departamento) {
		this.departamento = departamento;
	}

	public Cargo getCargo() {
		return cargo;
	}

	public void setCargo(Cargo cargo) {
		this.cargo = cargo;
	}

	public PerfilAcesso getPerfilAcesso() {
		return perfilAcesso;
	}

	public void setPerfilAcesso(PerfilAcesso perfilAcesso) {
		this.perfilAcesso = perfilAcesso;
	}

	public String getDisplayNome() {
		return this.nome != null ? this.nome.split("\\s+")[0] : "";
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((usuarioId == null) ? 0 : usuarioId.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Usuario other = (Usuario) obj;
		if (usuarioId == null) {
			if (other.usuarioId != null) {
				return false;
			}
		} else if (!usuarioId.equals(other.usuarioId)) {
			return false;
		}
		return true;
	}
	
}
